/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : Expression.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Apr 5, 2009  Raghu Sarraff	 		        Created
 */
package net.lwf.entity;

import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.handler.util.MathUtil;


public class Expression {
	
	private String answer = "";
	private String expression;
	private String operation;
	private int[] expnumerals;

	/**
	 * @param operation
	 * @param expnumerals
	 */
	public Expression(String operation, int[] expnumerals) {
		this.operation = operation;
		this.expnumerals = expnumerals;
		processExpression();
	}
	
	/**
	 * @return Returns the expnumerals.
	 */
	public int[] getExpnumerals() {
		return expnumerals;
	}
	/**
	 * @return Returns the operation.
	 */
	public String getOperation() {
		return operation;
	}
	

	/**
	 * @return Returns the answer.
	 */
	public String getAnswer() {
		return answer;
	}
	/**
	 * @param answer The answer to set.
	 */
	public void setAnswer(String answer) {
		this.answer = answer;
	}
	/**
	 * @return Returns the expression.
	 */
	public String getExpression() {
		//processExpression();
		return this.expression;
	}
	
	private void processExpression() {
		this.expression = "";
		int addanswer = 0;

		for(int i=0; i<expnumerals.length; i++) {
			this.expression += String.valueOf(expnumerals[i]);
	        if(i+1 < expnumerals.length) {
	        	this.expression += " " + MathUtil.getOperationsign(this.operation) + " ";
	        }
	        processAnswer(expnumerals);
	        
		}
		this.expression = "(" + this.expression + ")";

	}
	
	private void processAnswer(int[] exprnumerals) {
		int answer = exprnumerals[0];
		for (int i = 1; i < expnumerals.length; i++) {
			if (operation.equals(IHandlerConstant.ADDITION)) {
				answer += exprnumerals[i];
			}
			if (operation.equals(IHandlerConstant.SUBTRACTION)) {
				answer = answer - exprnumerals[i];
			}
			if (operation.equals(IHandlerConstant.MULTIPLICATION)) {
				answer = answer * exprnumerals[i];
			}
			if (operation.equals(IHandlerConstant.DIVISION)) {
				answer = answer / exprnumerals[i];
			}
		}
		this.answer = String.valueOf(answer);

	}
    
} //Expression

